home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / rdcf.exe / FILES.C < prev    next >
C/C++ Source or Header  |  1993-01-15  |  14KB  |  525 lines

  1. /* example of file access with RDCF 2.0 and CACHE 1.1 */
  2. /* public domain - no restrictions on use */
  3. /* Plain Vanilla Corporation - January 15, 1993 */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <process.h>
  9. #include <dos.h>
  10. #include <io.h>
  11. #include <alloc.h>
  12. #include "rdcf2.h"
  13. #include "cache.h"
  14.  
  15. static unsigned drive_access(int write, unsigned drive, unsigned LSN,
  16.   void *buffer)
  17. {
  18.   _SI; _DI;
  19.   return write ? abswrite(drive, 1, LSN, buffer) :
  20.     absread(drive, 1, LSN, buffer);
  21. }
  22.  
  23. struct cache *cache;
  24.  
  25. static unsigned cached_drive_access(int write, unsigned drive,
  26.   unsigned sector, void *buffer)
  27. {
  28.   return write && drive > 1 ? 1 :
  29.     cache_access(cache, write, drive, sector, buffer);
  30. }
  31.  
  32. static char *ERROR_MESSAGE[] =
  33. {
  34.   "NO ERROR           ",
  35.   "ACCESS DENIED      ",
  36.   "DIRECTORY CONFLICT ",
  37.   "DIRECTORY FULL     ",
  38.   "DISK FORMAT ERROR  ",
  39.   "DRIVE ERROR        ",
  40.   "FILE FORMAT ERROR  ",
  41.   "INVALID DIRECTORY  ",
  42.   "INVALID SPEC       ",
  43.   "MODE ERROR         ",
  44.   "FILE NOT FOUND     ",
  45.   "RENAMING ERROR     ",
  46.   "SEEK OUT OF RANGE  ",
  47.   "UNRECOVERABLE FILE "
  48. };
  49.  
  50. static char HELP[] =
  51.   "  COPY     source  destination\n"
  52.   "  DATE     file  month-day-year  hour:min:sec\n"
  53.   "  DEL      file\n"
  54.   "  DIR      directory\n"
  55.   "  DIRSORT  directory\n"
  56.   "  EXIT\n"
  57.   "  HELP\n"
  58.   "  MD       directory\n"
  59.   "  MCOPY    files directory\n"
  60.   "  MOVE     source  destination\n"
  61.   "  RECOVER  file\n"
  62.   "  REN      old  new\n"
  63.   "  TYPE     file\n"
  64.   "  UNDEL    file\n"
  65.   "  VOLUME   name\n"
  66.   "  WIPE     drive\n";
  67.  
  68. static void rdcf_error_message(int n)
  69. {
  70.   if (n == RDCF_DRIVE_ERROR)
  71.     printf("DRIVE ERROR ON DRIVE %c:\n", cache->error_drive + 'A');
  72.   else
  73.     puts(ERROR_MESSAGE[-n]);
  74. }
  75.  
  76. static void disk_write_error(void)
  77. {
  78.   printf("Disk write error on drive %c:\n", cache->error_drive + 'A');
  79. }
  80.  
  81. void rdcf_get_date_and_time(struct rdcf_date_and_time *p)
  82. {
  83.   struct date d;
  84.   struct time t;
  85.   int day;
  86.   getdate(&d);
  87.   day = d.da_day;
  88.   gettime(&t);
  89.   getdate(&d);
  90.   if (day != d.da_day) gettime(&t);
  91.   p->month = d.da_mon;
  92.   p->day = d.da_day;
  93.   p->year = d.da_year;
  94.   p->hour = t.ti_hour;
  95.   p->minute = t.ti_min;
  96.   p->second = t.ti_sec;
  97. }
  98.  
  99. static unsigned char scratch_buffer[RDCF_SECTOR_SIZE];
  100. struct rdcf f = {scratch_buffer, cached_drive_access};
  101. struct rdcf g = {scratch_buffer, cached_drive_access};
  102. static char buffer[1024];
  103.  
  104. static void three_numbers(char *s, unsigned *n1, unsigned *n2, unsigned *n3)
  105. {
  106.   if (isdigit(*s))
  107.   {
  108.     *n1 = 0;
  109.     do *n1 = 10 * (*n1) + *s++ - '0'; while (isdigit(*s));
  110.   }
  111.   if (*s != 0) s++;
  112.   if (isdigit(*s))
  113.   {
  114.     *n2 = 0;
  115.     do *n2 = 10 * (*n2) + *s++ - '0'; while (isdigit(*s));
  116.   }
  117.   if (*s != 0) s++;
  118.   if (isdigit(*s))
  119.   {
  120.     *n3 = 0;
  121.     do *n3 = 10 * (*n3) + *s++ - '0'; while (isdigit(*s));
  122.   }
  123. }
  124.  
  125.  
  126. static int break_flag;
  127.  
  128. static void interrupt Ctrl_C_handler() {break_flag = 1;}
  129.  
  130. static int copy(char *source, char *destination)
  131. {
  132.   if (rdcf_open(&f, source, RDCF_READ))
  133.   {
  134.     rdcf_error_message(f.result);
  135.     return 0;
  136.   }
  137.   if (rdcf_open(&g, destination, RDCF_CREATE))
  138.   {
  139.     rdcf_error_message(g.result);
  140.     return 0;
  141.   }
  142.   while (!break_flag)
  143.   {
  144.     int n = rdcf_read(&f, buffer, sizeof(buffer));
  145.     if (n<0)
  146.     {
  147.       rdcf_error_message(n);
  148.       return 0;
  149.     }
  150.     if (n==0) break;
  151.     n = rdcf_write(&g, buffer, n);
  152.     if (n<0)
  153.     {
  154.       rdcf_error_message(n);
  155.       return 0;
  156.     }
  157.     if (n==0) break;
  158.   }
  159.   rdcf_close(&f);
  160.   rdcf_close(&g);
  161.   rdcf_date_and_time(&g, destination, &f.file.date_and_time);
  162.   return 1;
  163. }
  164.  
  165. static char *directory_and_specs(char *directory, char *specs)
  166. {
  167.   static char s[82];
  168.   char *p = s;
  169.   int c;
  170.   while (*directory != 0) c = *p++ = *directory++;
  171.   if (c != ':') *p++ = '\\';
  172.   strcpy(p, specs);
  173.   return s;
  174. }
  175.  
  176. static void command(void)
  177. {
  178.   char *arg[4];
  179.   {
  180.     static struct
  181.     {
  182.       unsigned char capacity;
  183.       unsigned char count;
  184.       char text[70];
  185.     } buffer = {sizeof(buffer.text), 0, '\r'};
  186.     static char buffer2[sizeof(buffer.text)];
  187.     unsigned i;
  188.     register char *p;
  189.     fputs("FILES>", stdout);
  190.     bdos(10, (unsigned)(&buffer), 0);
  191.     bdos(2, '\n', 0);
  192.     for (i=0, p=buffer2; i<buffer.count; i++)
  193.       *p++ = toupper(buffer.text[i]);
  194.     *p = 0;
  195.     p = buffer2;
  196.     for (i=0; i<4; i++)
  197.     {
  198.       while (*p==' ' || *p=='\t') p++;
  199.       arg[i] = p;
  200.       while (*p!=' ' && *p!='\t' && *p!=0) p++;
  201.       if (*p!=0) *p++ = 0;
  202.     }
  203.   }
  204.   if (strcmp(arg[0],"COPY")==0)
  205.   {
  206.     break_flag = 0;
  207.     copy (arg[1], arg[2]);
  208.   }
  209.   else if (strcmp(arg[0],"DATE")==0)
  210.   {
  211.     unsigned month = 1;
  212.     unsigned day = 1;
  213.     unsigned year = 1980;
  214.     unsigned hour = 0;
  215.     unsigned minute = 0;
  216.     unsigned second = 0;
  217.     struct rdcf_date_and_time d;
  218.     three_numbers(arg[2], &month, &day, &year);
  219.     d.month = month;
  220.     d.day = day;
  221.     if (80 <= year && year <= 99) year += 1900;
  222.     else if (year < 70) year += 2000;
  223.     d.year = year;
  224.     three_numbers(arg[3], &hour, &minute, &second);
  225.     d.hour = hour;
  226.     d.minute = minute;
  227.     d.second = second;
  228.     if (rdcf_date_and_time(&f, arg[1], &d) != 0)
  229.     {
  230.       rdcf_error_message(f.result);
  231.       return;
  232.     }
  233.   }
  234.   else if (strcmp(arg[0],"DEL")==0)
  235.   {
  236.     if (rdcf_delete(&f, arg[1]) != 0)
  237.     {
  238.       rdcf_error_message(f.result);
  239.       return;
  240.     }
  241.   }
  242.   else if (strcmp(arg[0],"DIR")==0)
  243.   {
  244.     static char drive[] = "X:";
  245.         struct
  246.         {
  247.       struct
  248.       {
  249.         unsigned current;
  250.         unsigned deleted;
  251.       } file, directory, volume;
  252.         } count;
  253.         memset(&count, 0, sizeof(count));
  254.     break_flag = 0;
  255.     if (rdcf_get_file_information(&f, arg[1], 0) != RDCF_DIRECTORY_FULL) do
  256.     {
  257.       if (f.result == RDCF_FILE_NOT_FOUND || f.result == 0)
  258.       {
  259.         if (f.result == RDCF_FILE_NOT_FOUND) f.file.spec[0] = '?';
  260.         printf("%-12s ", f.file.spec);
  261.         if (f.file.attribute & RDCF_DIRECTORY)
  262.                 {
  263.                     if (f.result == 0) count.directory.current++;
  264.           else count.directory.deleted++;
  265.           printf("   <DIR> ");
  266.                 }
  267.         else if (f.file.attribute & RDCF_VOLUME)
  268.                 {
  269.                     if (f.result == 0) count.volume.current++;
  270.           else count.volume.deleted++;
  271.           printf("   <VOL> ");
  272.                 }
  273.                 else
  274.                 {
  275.                     if (f.result == 0) count.file.current++;
  276.           else count.file.deleted++;
  277.           printf("%8ld ", f.file.size);
  278.                 }
  279.         printf("%c%c%c%c%c%c ",
  280.           f.file.attribute&RDCF_ARCHIVE   ? 'A' : ' ',
  281.           f.file.attribute&RDCF_DIRECTORY ? 'D' : ' ',
  282.           f.file.attribute&RDCF_HIDDEN    ? 'H' : ' ',
  283.           f.file.attribute&RDCF_READ_ONLY ? 'R' : ' ',
  284.           f.file.attribute&RDCF_SYSTEM    ? 'S' : ' ',
  285.           f.file.attribute&RDCF_VOLUME    ? 'V' : ' ');
  286.         printf("%02d-%02d-%04d %02d:%02d:%02d%s\n",
  287.           f.file.date_and_time.month, f.file.date_and_time.day,
  288.           f.file.date_and_time.year, f.file.date_and_time.hour,
  289.           f.file.date_and_time.minute, f.file.date_and_time.second,
  290.           f.result == RDCF_FILE_NOT_FOUND ? " DELETED" : "");
  291.       }
  292.       else
  293.       {
  294.         rdcf_error_message(f.result);
  295.         return;
  296.       }
  297.     } while (rdcf_next_file_information(&f) != RDCF_DIRECTORY_FULL &&
  298.       !break_flag);
  299.     printf("\n                current  deleted   total\n");
  300.     printf("                -------  -------  -------\n");
  301.     printf("  files         %7d  %7d  %7d\n", count.file.current,
  302.             count.file.deleted, count.file.current+count.file.deleted);
  303.     printf("  directories   %7d  %7d  %7d\n", count.directory.current,
  304.             count.directory.deleted, count.directory.current+count.directory.deleted);
  305.     printf("  volume labels %7d  %7d  %7d\n", count.volume.current,
  306.             count.volume.deleted, count.volume.current+count.volume.deleted);
  307.     printf("                -------  -------  -------\n");
  308.     printf("  total         %7d  %7d  %7d\n",
  309.       count.file.current+count.directory.current+count.volume.current,
  310.